package cc.monggo.third.impl;

import java.util.HashMap;

import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;
import cc.monggo.third.SinaService;
import cc.monggo.third.client.ThirdAuthClient;
import cc.monggo.third.model.SinaAccessToken;
import cc.monggo.third.model.SinaUser;

public class SinaServiceImpl implements SinaService {

	@Override
	public SinaAccessToken getAccessToken(HashMap<String,String> params) {
		SinaAccessToken accessToken = new SinaAccessToken();
		String body = ThirdAuthClient.getAccessToken(params);
		try{
			JSONObject jsonO = new JSONObject(body);
			accessToken.setAccessToken(jsonO.getString("access_token"));
			accessToken.setExpiresIn(jsonO.getString("expires_in"));
			accessToken.setUid(jsonO.getString("uid"));			
			return accessToken;
		}catch(JSONException e){
			e.printStackTrace();
		}	
		return null;
	}

	@Override
	public SinaUser getUser(HashMap<String,String> params) {
		SinaUser sinaUser = new SinaUser();
		String body = ThirdAuthClient.getUser(params);
		try{
			JSONObject jsonO = new JSONObject(body);
			sinaUser.setSrceen_name(jsonO.getString("screen_name"));
			sinaUser.setProfileImageUrl(jsonO.getString("profile_image_url"));	
			sinaUser.setProfileImageUrl_large(jsonO.getString("avatar_large"));
			return sinaUser;
		}catch(JSONException e){
			e.printStackTrace();
		}	
		return null;
	}

}
